﻿Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Collections.Generic

Imports DotNetNuke
Imports DotNetNuke.Security
Imports Insurance
Imports DotNetNuke.Entities.Modules

Namespace DotNetNuke.Modules.Insurance
    Partial Class EditInsurance
        Inherits PortalModuleBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim iid As Integer = 0

            If Not Page.IsPostBack Then
                'Load data for dropdown list
                LoadDataForDDL()
                'Register event for delete buton
                Dim strMessage = "Do you sure delete this item?"
                btnDelete.Attributes.Add("onclick", String.Format("return confirm('{0}')", strMessage))
                If Not Request.Params("iid") Is Nothing Then
                    iid = Integer.Parse(Request.Params("iid"))
                    btnDelete.Visible = True
                    btnAddNew.Text = "Update"
                    ltrHeading.Text = "Update insurances Accepted"
                    'Load data info
                    LoadDataInfo(iid)
                Else
                    btnDelete.Visible = False
                    btnAddNew.Text = "Add new"
                End If

            End If

        End Sub

        Private Sub LoadDataInfo(ByVal iid As Integer)
            Dim context As InsuranceDataContext = New InsuranceDataContext()
            Dim query = (From p In context.cwc_Insurances Where p.insuranceID = iid _
                         Select New With {p.company, p.secondaryMessage, p.ParentID}).FirstOrDefault()

            If Not query Is Nothing Then
                txtCompany.Text = query.company
                txtSecondMsg.Text = query.secondaryMessage

                If query.ParentID.HasValue Then
                    ddlParent.SelectedValue = query.ParentID.Value
                End If

            End If

        End Sub

        Private Sub LoadDataForDDL()
            Dim context As InsuranceDataContext = New InsuranceDataContext()
            Dim query = (From p In context.cwc_Insurances _
                         Order By p.company _
                         Select New With {p.insuranceID, p.company}).ToList()
            ddlParent.DataSource = query
            ddlParent.DataTextField = "company"
            ddlParent.DataValueField = "insuranceID"
            ddlParent.DataBind()
            Dim item As New ListItem()
            item.Value = "-1"
            item.Text = "Select parent company"
            ddlParent.Items.Insert(0, item)

        End Sub

        Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Me.Response.Redirect("Default.aspx?TabId=" + Me.TabId.ToString + "&go=ManageInsurance")
        End Sub

        ''' <summary>
        ''' Delete action
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Try
                Dim context As InsuranceDataContext = New InsuranceDataContext()
                Dim iid As Integer = Convert.ToInt32(Request.Params("iid"))
                Dim objectInsurance = (From p In context.cwc_Insurances Where p.insuranceID = iid Select p).FirstOrDefault()

                If Not objectInsurance Is Nothing Then
                    context.cwc_Insurances.DeleteOnSubmit(objectInsurance)
                    context.SubmitChanges()
                    Me.Response.Redirect("Default.aspx?TabId=" + Me.TabId.ToString + "&go=ManageInsurance")
                End If
            Catch
                lblMsg.Text = "Has a error when delete this item."
            End Try
            

        End Sub

        ''' <summary>
        ''' Add action
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
            Try
                Dim context As InsuranceDataContext = New InsuranceDataContext()
                Dim objectInsurance As New cwc_Insurance()
                If Not Request.Params("iid") Is Nothing Then
                    Dim iid As Integer = Convert.ToInt32(Request.Params("iid"))
                    objectInsurance = (From p In context.cwc_Insurances Where p.insuranceID = iid Select p).FirstOrDefault()
                    If Not objectInsurance Is Nothing Then
                        objectInsurance.company = txtCompany.Text.Trim()

                        If txtSecondMsg.Text.Trim.Length > 1000 Then
                            lblMsg.Text = "Max length isn't more greater than 1000 characters "
                            divEror.Visible = True
                            Exit Sub
                        Else
                            objectInsurance.secondaryMessage = txtSecondMsg.Text.Trim()
                        End If

                        If ddlParent.SelectedIndex <> 0 Then
                            objectInsurance.ParentID = CType(ddlParent.SelectedValue, Integer)
                        End If

                        context.SubmitChanges()
                        lblMsg.Text = "Has updated item successfull."
                        divEror.Visible = True
                    End If

                Else
                    objectInsurance.company = txtCompany.Text.Trim()
                    If txtSecondMsg.Text.Trim.Length > 1000 Then
                        lblMsg.Text = "Max length isn't more greater than 1000 characters "
                        divEror.Visible = True
                        Exit Sub
                    Else
                        objectInsurance.secondaryMessage = txtSecondMsg.Text.Trim()
                    End If
                    If ddlParent.SelectedIndex <> 0 Then
                        objectInsurance.ParentID = CType(ddlParent.SelectedValue, Integer)
                    End If
                    context.cwc_Insurances.InsertOnSubmit(objectInsurance)
                    context.SubmitChanges()
                    txtCompany.Text = ""
                    txtSecondMsg.Text = ""
                    lblMsg.Text = "Has added new item successfull."
                    divEror.Visible = True
                End If
            Catch
                If Not Request.Params("iid") Is Nothing Then
                    lblMsg.Text = "Has a error when update item."
                Else
                    lblMsg.Text = "Has a error when add new item."
                End If
                divEror.Visible = True
            End Try


        End Sub
    End Class

End Namespace
